﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Validation;
using System.Diagnostics;
using System.Linq;
using System.Web.Security;
using KeyCore.Domain.Model;
using KeyCore.Framework.Contexts;

namespace KeyCore.Framework.Repositories
{
    public class LoansClientRepository : ILoansClientRepository
    {
        private ILoansClientContext _loansClientContext;
      //  private Guid _clientId;


        public LoansClientRepository()
        {
            _loansClientContext = new LoansClientContext();
       //     (_loansClientContext as DbContext).Configuration.LazyLoadingEnabled = true;
       //    (_loansClientContext as DbContext).Configuration.ProxyCreationEnabled= true;


        }

        public void Dispose()
        {
            if (_loansClientContext != null)
                _loansClientContext.Dispose();
        }

        public DbContext Context
        {
            get { return _loansClientContext as DbContext; }
        }

      //  public bool ClientExist
      //  {
      //      get
      //      {    
      //          return ChceckClientExist();
      //      }
      //  }

      //  public Guid ClientId
      //  {
      //      get { return (Guid)Membership.GetUser().ProviderUserKey; }
      //  }

      //  public bool ClientPersonalDataExist
      //  {
      //      get
      //      {
      //          if (GetLoansClient(ClientId). != null) return true;
      //          return false;
      //      }
      //  }

      //  private bool ChceckClientExist()
      //  {
      //      if (Membership.GetUser() != null)
      //      {
      //          _clientId = ClientId;
      //          if (GetLoansClient(_clientId) != null)
      //          {
      //              return true;
      //          }
      //      }

      //      return false;
      //  }

        public IEnumerable<LoansClientContext> GetAll()
        {
            return (_loansClientContext as DbContext).Set<LoansClientContext>().ToList();
        }

        public IList<LoansClient> GetC()
        {
            var q = from c in _loansClientContext.LoansClients select c;
            return q.ToList();
        }

        public void CreateLoansClient(LoansClient loansClient)
        {
            //     (_loansClientContext as DbContext).UpdateGraph(loansClient.DaneOsobowe);
            _loansClientContext.LoansClients.Add(loansClient);
            try { (_loansClientContext as DbContext).SaveChanges(); }
            catch (DbEntityValidationException dbEx)
            {
                foreach (var validationErrors in dbEx.EntityValidationErrors)
                {
                    foreach (var validationError in validationErrors.ValidationErrors)
                    {
                        Trace.TraceInformation("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage);
                    }
                }
            }
        }

        public LoansClient GetLoansClient(Guid loansClientId)
        {
            var query = from c in _loansClientContext.LoansClients where c.OsobaId == loansClientId select c;
            return query.SingleOrDefault();
        }

        public IQueryable<AdresTyp> GetAllAdresTyp()
        {
            var query = from a in _loansClientContext.AdresTyp select a;
            return query;
        }

        public IQueryable<TelefonTyp> GetAllTelefonTyp()
        {
            var query = from a in _loansClientContext.TelefonTyp select a;
            return query;
        }

        public IQueryable<TelefonPrefix> GetAllTelefonPrefix()
        {
            var query = from a in _loansClientContext.TelefonPrefix select a;
            return query;
        }

        public IQueryable<Kraj> GetAllKraj()
        {
            var query = from a in _loansClientContext.Kraj select a;
            return query;
        }

        public IQueryable<Miejscowosc> GetAllMiejscowosc()
        {
            var query = from a in _loansClientContext.Miejscowosc select a;
            return query;
        }

        public IQueryable<Wojewodztwo> GetAllWojewodztwo()
        {
            var query = from a in _loansClientContext.Wojewodztwos select a;
            return query;
        }

        public IQueryable<Bank> GetAllBank()
        {
            var query = from a in _loansClientContext.Bank select a;
            return query;
        }

        public IQueryable<TypDokumentu> GetAllTypDokumentu()
        {
            var query = from a in _loansClientContext.TypDokumentu select a;
            return query;
        }

        //public void LoansClientAddAdress(Guid loansClientId, Adres adres)
        //{
        //    throw new NotImplementedException();
        //}
    }
}